using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using IdSharp.Tagging.ID3v2.Frames;

namespace Media_Renamer {
    public partial class MediaFileWindow : LocalizedForm {
        private MP3File[] _files;
        private AutoCompleteStringCollection listArtist;
        private AutoCompleteStringCollection listAlbum;
        private AutoCompleteStringCollection listYear;
        private AutoCompleteStringCollection listTitle;
        private AutoCompleteStringCollection listGenre;


	
        public MediaFileWindow() {
            InitializeComponent();
            listArtist = new AutoCompleteStringCollection();
            listAlbum = new AutoCompleteStringCollection();
            listYear = new AutoCompleteStringCollection();
            listTitle = new AutoCompleteStringCollection();
            listGenre = new AutoCompleteStringCollection();
            this.txtArtist.AutoCompleteCustomSource = listArtist;
            this.txtAlbum.AutoCompleteCustomSource = listAlbum;
            this.txtYear.AutoCompleteCustomSource = listYear;
            this.txtTitle.AutoCompleteCustomSource = listTitle;
            this.txtGenre.AutoCompleteCustomSource = listGenre;
            setUpCulture();
        }
        public override void setUpCulture() {
            this.buttonCancel.Text = this.getString("mediaFileWindow.btnCancel");
            this.buttonOk.Text = this.getString("mediaFileWindow.btnSave");
            this.chkAlbum.Text = this.getString("mediaFileWindow.chkAlbum");
            this.chkArtist.Text =  this.getString("mediaFileWindow.chkArtist");
            this.chkGenre.Text = this.getString("mediaFileWindow.chkGenre");
            this.chkImages.Text = this.getString("mediaFileWindow.chkImages");
            this.chkTitle.Text = this.getString("mediaFileWindow.chkTitle");
            this.chkTrackNo.Text = this.getString("mediaFileWindow.chkTrackNo");
            this.chkYear.Text = this.getString("mediaFileWindow.chkYear");
            this.lblHelp.Text = this.getString("mediaFileWindow.help");
            this.lblHelpImages.Text = this.getString("mediaFileWindow.images.help");
            
            this.Text = this.getString("mediaFileWindow.title");
        }
	

        public MP3File[] Files {
            get { return _files; }
            set { _files = value;
            listArtist.Clear();
            listAlbum.Clear();
            listYear.Clear();
            listTitle.Clear();
            listGenre.Clear();
            if (_files.Length == 1) {
                if (_files[0].UsingV1)
                {
                    foreach (IAttachedPicture picture in _files[0].Id3v2Tag.PictureList) {
                        Image image = picture.Picture;
                        image.Tag = picture.MimeType;
                        imageSlide1.addImage(image);
                    }
                }
                
            }
            foreach (MP3File mediaFile in _files) {
                string artist="", album="", title="", year="", genre="";
                if (mediaFile.UsingV1)
                {
                    artist = mediaFile.Id3v1Tag.Artist;
                    album = mediaFile.Id3v1Tag.Album;
                    title = mediaFile.Id3v1Tag.Title;
                    year = mediaFile.Id3v1Tag.Year;
                    genre = mediaFile.Id3v1Tag.GenreIndex.ToString();
                } else {
                    artist = mediaFile.Id3v2Tag.Artist;
                    album = mediaFile.Id3v2Tag.Album;
                    title = mediaFile.Id3v2Tag.Title;
                    year = mediaFile.Id3v2Tag.Year;
                    genre = mediaFile.Id3v2Tag.Genre;
                }
                if (artist !=null && artist.Length > 0) listArtist.Add(artist);
                if (album != null && album.Length > 0) listAlbum.Add(album);
                if (title != null && title.Length > 0) listTitle.Add(title);
                if (year != null && year.Length > 0) listYear.Add(year);
                if (genre != null && genre.Length > 0) listGenre.Add(genre);
            }
            this.txtArtist.AutoCompleteCustomSource = listArtist;
            this.txtAlbum.AutoCompleteCustomSource = listAlbum;
            this.txtYear.AutoCompleteCustomSource = listYear;
            this.txtTitle.AutoCompleteCustomSource = listTitle;
            this.txtGenre.AutoCompleteCustomSource = listGenre;
                
            }
        }
        private void refreshEnabled(object sender, EventArgs e) {
            refreshEnabled();
        }
        private void refreshEnabled() {
            txtTitle.Enabled = chkTitle.Checked;
            txtAlbum.Enabled = chkAlbum.Checked;
            txtArtist.Enabled = chkArtist.Checked;
            txtGenre.Enabled = chkGenre.Checked;
            txtTrackNo.Enabled = chkTrackNo.Checked;
            txtYear.Enabled = chkYear.Checked;

        }
        public String Title {
            get { return txtTitle.Text; }
            set { txtTitle.Text = value; }
        }

        public bool changeTitle {
            get { return chkTitle.Checked; }
            set { chkTitle.Checked = value; }
        }

        public String Album {
            get { return txtAlbum.Text; }
            set { txtAlbum.Text = value; }
        }
        public bool changeAlbum {
            get { return chkAlbum.Checked; }
            set { chkAlbum.Checked = value; }
        }

        public String Artist {
            get { return txtArtist.Text; }
            set { txtArtist.Text = value; }
        }
        public bool changeArtist {
            get { return chkArtist.Checked; }
            set { chkArtist.Checked = value; }
        }

        public String Genre {
            get { return txtGenre.Text; }
            set { txtGenre.Text = value; }
        }
        public bool changeGenre {
            get { return chkGenre.Checked; }
            set { chkGenre.Checked = value; }
        }

        public String TrackNo {
            get { return txtTrackNo.Text; }
            set { txtTrackNo.Text = value; }
        }
        public bool changeTrackNo {
            get { return chkTrackNo.Checked; }
            set { chkTrackNo.Checked = value; }
        }

        public String Year {
            get { return txtYear.Text; }
            set { txtYear.Text = value; }
        }
        public bool changeYear {
            get { return chkYear.Checked; }
            set { chkYear.Checked = value; }
        }

        private void buttonOk_Click(object sender, EventArgs e) {
            this.barProgress.Maximum = _files.Length;
            this.barProgress.Visible = true;
            setButtonsEnabled(false);
            imageSlide1.FreeImage();
            foreach (MP3File mediaFile in _files) {
                if (mediaFile.UsingV1)
                {
                    if (this.changeAlbum) mediaFile.Id3v1Tag.Album = this.Album;
                    if (this.changeArtist) mediaFile.Id3v1Tag.Artist = this.Artist;
                    if (this.changeGenre) mediaFile.Id3v1Tag.GenreIndex = Int16.Parse(this.Genre); //TODO: This should manage a combo id
                    if (this.changeTitle) mediaFile.Id3v1Tag.Title = this.Title;
                    if (this.changeTrackNo) mediaFile.Id3v1Tag.TrackNumber = Int16.Parse(this.TrackNo); //TODO: This should only allow numbers if v1
                    if (this.changeYear) mediaFile.Id3v1Tag.Year = this.Year;
                } else {
                    if (this.changeAlbum) mediaFile.Id3v2Tag.Album = this.Album;
                    if (this.changeArtist) mediaFile.Id3v2Tag.Artist = this.Artist;
                    if (this.changeGenre) mediaFile.Id3v2Tag.Genre = this.Genre;
                    if (this.changeTitle) mediaFile.Id3v2Tag.Title = this.Title;
                    if (this.changeTrackNo) mediaFile.Id3v2Tag.TrackNumber = this.TrackNo;
                    if (this.changeYear) mediaFile.Id3v2Tag.Year = this.Year;
                    if (this.chkImages.Checked) {
                        mediaFile.Id3v2Tag.PictureList.Clear();
                        foreach (Image image in imageSlide1.getImages()) {
                            IAttachedPicture picture = mediaFile.Id3v2Tag.PictureList.AddNew();
                            System.Drawing.Imaging.ImageFormat format = new System.Drawing.Imaging.ImageFormat(image.RawFormat.Guid);
                            if (((String)image.Tag).ToLower().EndsWith("png")) {
                                picture.MimeType = "image/png";

                            } else {
                                picture.MimeType = "image/jpeg";

                            }


                            Image temp = new Bitmap(image.Width, image.Height);
                            Graphics graph = Graphics.FromImage(temp);
                            graph.DrawImage(image, 0, 0);
                            image.Dispose();
                            graph.Dispose();
                            System.IO.MemoryStream mem = new System.IO.MemoryStream();
                            temp.Save(mem, format);
                            picture.Picture = Image.FromStream(mem);
                        }
                    }
                }
                mediaFile.saveInfo();
                barProgress.Value += 1;
                Application.DoEvents();
            }
            imageSlide1.Clear();
        }
        private void setButtonsEnabled(Boolean enabled) {
            this.buttonCancel.Enabled = enabled;
            this.buttonOk.Enabled = enabled;
        }
        private void menuDelete_Click(object sender, EventArgs e) {
            imageSlide1.Delete();
        }


	

    }
}